<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="hAssign(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEsit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDal(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="roleForm.page"
                :page-sizes="[2, 4, 8, 16]"
                :page-size="roleForm.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹层 -->
      <el-dialog
        :title="isEsit?'编辑':'新增'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form ref="hroleForm" :model="hroleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="hroleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="hroleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限弹层 -->
      <el-dialog
        title="分配权限'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showPermission"
      >
        <AssignPermission v-if="showPermission" :cur-id="curId" @success="showPermission = false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRole, addRole, putRole, deleteRole } from '@/api/setting'
import AssignPermission from './assignPermission.vue'
export default {
  name: 'VueWhRzSettings',
  components: {
    AssignPermission
  },

  data() {
    return {
      showPermission: false, // 分配权限弹层
      curId: '',
      roleForm: {
        page: 1, // 页码值
        pagesize: 2 // 每页显示几条数据
      },
      hroleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      list: [], // 保存角色列表
      total: 0, // 总的数据条数
      showDialog: false,
      isEsit: false // false新增 || true编辑
    }
  },

  created() {
    this.lodegetRole()
  },

  methods: {
    // 传给 assignPermission 组件的id
    hAssign(id) {
      this.showPermission = true
      this.curId = id
    },
    hSubmit() {
    // 兜底校验
      this.$refs.hroleForm.validate((valid) => {
        if (!valid) return
        this.isEsit ? this.doEsit() : this.doAdd()
      })
    },
    // 重置校验规则,表单数据
    hClose() {
      this.$refs.hroleForm.resetFields()
      this.hroleForm = {
        name: '',
        description: ''
      }
    },
    hEsit(row) {
      this.showDialog = true
      this.isEsit = true
      this.hroleForm = { ...row }
    },
    hAdd() {
      this.showDialog = true
      this.isEsit = false
    },
    // 删除
    async hDal(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await deleteRole(id)
          // console.log(res, '删除')
          if (res.code === 10000) {
            this.$message.success(res.message)
            if (this.list.length === 1) {
              this.roleForm.page--
              if (this.roleForm.page <= 0) {
                this.roleForm.page = 1
              }
            }

            this.lodegetRole()
          }
        } catch (error) {
          // console.log(error)
          this.$message.error(error)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑
    async  doEsit() {
      try {
        const res = await putRole(this.hroleForm)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.showDialog = false
        this.lodegetRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 新增
    async doAdd() {
      try {
        const res = await addRole(this.hroleForm)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.showDialog = false
        this.total++
        const max = Math.ceil(this.total / this.roleForm.pagesize)
        this.roleForm.page = max
        this.lodegetRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取角色列表
    async lodegetRole() {
      try {
        const res = await getRole(this.roleForm)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.roleForm.pagesize = val
      this.roleForm.page = 1
      this.lodegetRole()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.roleForm.page = val
      this.lodegetRole()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
